/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Persistence;
import javax.persistence.Query;

@Entity
public class Persona implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "Cedula")
    private Long id;
    private String mail;
    private String password;
    private String nombre;
    private float notaPromedio;
    private Long semestre;
    private String programa;
    @ManyToOne
    private Fuente fuente;

    public Fuente getFuente() {
        return fuente;
    }

    public void setFuente(Fuente fuente) {
        this.fuente = fuente;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public float getNotaPromedio() {
        return notaPromedio;
    }

    public void setNotaPromedio(float notaPromedio) {
        this.notaPromedio = notaPromedio;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Long getSemestre() {
        return semestre;
    }

    public void setSemestre(Long semestre) {
        this.semestre = semestre;
    }

    public String getPrograma() {
        return programa;
    }

    public void setPrograma(String programa) {
        this.programa = programa;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Persona)) {
            return false;
        }
        Persona other = (Persona) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Modelo.Persona[id=" + id + "]";
    }

    public Persona findPersona(Long id) {


        Persona persona = new Persona();

        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
                "APU");

        // Crear el EntityManager para accesso a la capa de persistencia
        EntityManager em = emf.createEntityManager();
        // Se es transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n de escritura, obtener la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        // del EntityManager e iniciarla
        em.getTransaction().begin();

        // Marcar el registro en la base de datos
        persona = em.find(Persona.class, id);

        // Ejecutar la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        em.getTransaction().commit();

        return persona;

    }

    public List validaLogin(String id, String clave, Long tipo) {
        List l=new ArrayList();
        boolean valor = false;
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("APU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();


        Query q = em.createQuery("SELECT p from Persona p WHERE p.id=:id and  p.password=:clave and p.fuente.id=:tipo");
        q.setParameter("id", Long.parseLong(id));
        q.setParameter("clave", clave);
        q.setParameter("tipo", tipo);
        em.getTransaction().commit();
        try {
            Persona p = (Persona) q.getSingleResult();
            l.add(p);
            System.out.print("entra");
        } catch (Exception ex) {
            l=null;
            System.out.print("nada");
        }

        return l;
    }

     public List fndAllPetsByName(String petName) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
                "APU");


        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();


       // Query q = em.createNamedQuery("Proyecto.sql");
   //     q.setParameter("pname", petName);
        //Persona p = new Persona();
        Query q = em.createNativeQuery(petName, Persona.class);

        return q.getResultList();
    }

     public List getListaPersonas() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("APU");
        EntityManager em = emf.createEntityManager();
        Query q = em.createQuery("SELECT p FROM Persona p");
        return q.getResultList();
    }

      public List getListaPersonasMias(Long id) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("APU");
        EntityManager em = emf.createEntityManager();
        Fuente f=em.find(Fuente.class, id);
        Query q = em.createQuery("SELECT p FROM Persona p WHERE p.fuente=:fu ");
        q.setParameter("fu", f);
        return q.getResultList();
    }

}
